package com.qsmx.store.app.main.mem;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.event.CardReadEvent;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.mem.entity.MemManagerEntity;
import com.qsmx.store.app.util.CardReadUtil;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.fusion.FusionCode;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class MemCardPropertyDelegate extends AtmosDelegate {

    private boolean isReadNow = false;
    private char mCardIcTag = 0x04;
    private MemManagerEntity mMemManagerData;
    private String icCardStatus;

    @BindView(R2.id.tv_mem_get_coin)
    AppCompatTextView tvMemGetCoin;
    @BindView(R2.id.tv_mem_save_coin)
    AppCompatTextView tvMemSaveCoin;
    @BindView(R2.id.tv_mem_save_ticket)
    AppCompatTextView tvMemSaveTicket;
    @BindView(R2.id.tv_mem_ticket_to_other)
    AppCompatTextView tvMemTicketToOther;
    @BindView(R2.id.tv_mem_point_set)
    AppCompatTextView tvMemPointSet;

    @BindView(R2.id.tv_ic_card_num)
    AppCompatTextView tvIcCardNum;
    @BindView(R2.id.tv_mem_card_num)
    AppCompatTextView tvMemCardNum;
    @BindView(R2.id.tv_mem_status)
    AppCompatTextView tvMemStatus;
    @BindView(R2.id.tv_phone_num)
    AppCompatTextView tvPhoneNum;
    @BindView(R2.id.tv_name)
    AppCompatTextView tvName;
    @BindView(R2.id.tv_level)
    AppCompatTextView tvLevel;

    @OnClick(R2.id.iv_card_scan)
    void onCardScan() {
        if (!isReadNow) {
            CardReadUtil.StartGetCardNum(getContext(), mCardIcTag, new CardReadEvent(new Bundle()));
            isReadNow = true;
        }
    }

    @OnClick(R2.id.tv_mem_get_coin)
    void onGetCoin() {
        onSetTitleCheckBg(tvMemGetCoin);
        getSupportDelegate().replaceChildFragment(MemCardPropertyGetCoinDelegate.create(mMemManagerData), false);
    }

    @OnClick(R2.id.tv_mem_save_coin)
    void onSaveCoin() {
        onSetTitleCheckBg(tvMemSaveCoin);
        getSupportDelegate().replaceChildFragment(MemCardPropertySaveCoinDelegate.create(mMemManagerData), false);
    }

    @OnClick(R2.id.tv_mem_save_ticket)
    void onSaveTicket() {
        onSetTitleCheckBg(tvMemSaveTicket);
        getSupportDelegate().replaceChildFragment(MemCardPropertySaveTicketDelegate.create(mMemManagerData), false);
    }

    @OnClick(R2.id.tv_mem_ticket_to_other)
    void onTicketToOther() {
        onSetTitleCheckBg(tvMemTicketToOther);
        getSupportDelegate().replaceChildFragment(new MemCardPropertyTicketToOtherDelegate(), false);
    }

    @OnClick(R2.id.tv_mem_point_set)
    void onPointSet() {
        onSetTitleCheckBg(tvMemPointSet);
        getSupportDelegate().replaceChildFragment(new MemCardPropertyPointSetDelegate(), false);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_property;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        onSetTitleCheckBg(tvMemGetCoin);
        getSupportDelegate().loadRootFragment(R.id.delegate_container, new MemCardPropertyGetCoinDelegate());

    }

    public void onSetTitleCheckBg(AppCompatTextView tv) {
        tvMemGetCoin.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_gary_no_bg));
        tvMemSaveCoin.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_gary_no_bg));
        tvMemSaveTicket.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_gary_no_bg));
        tvMemTicketToOther.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_gary_no_bg));
        tvMemPointSet.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_gary_no_bg));

        tvMemGetCoin.setTextColor(getResources().getColor(R.color.theme_black));
        tvMemSaveCoin.setTextColor(getResources().getColor(R.color.theme_black));
        tvMemSaveTicket.setTextColor(getResources().getColor(R.color.theme_black));
        tvMemTicketToOther.setTextColor(getResources().getColor(R.color.theme_black));
        tvMemPointSet.setTextColor(getResources().getColor(R.color.theme_black));

        tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_theme_bg));
        tv.setTextColor(getResources().getColor(R.color.theme_white));
    }

    private void onGetCardInfo(String cardNum) {
        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("deptId", FustionManager.mMachineData.getDeptId());
        params.put("icCardNo", cardNum);

        HttpHelper.RestClientGet(params, HttpUrl.MEM_CARD_INFO,
                new ISuccess() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<MemManagerEntity>() {
                        }.getType();
                        Gson gson = new Gson();
                        mMemManagerData = gson.fromJson(response, type);
                        if (mMemManagerData.getIcCard().getIcStatus() == 0) {
                            icCardStatus = "发新卡";
                        } else if (mMemManagerData.getIcCard().getIcStatus() == 1) {
                            icCardStatus = "正常";
                        } else if (mMemManagerData.getIcCard().getIcStatus() == 2) {
                            icCardStatus = "暂停使用";
                        } else if (mMemManagerData.getIcCard().getIcStatus() == 3) {
                            icCardStatus = "锁卡";
                        } else if (mMemManagerData.getIcCard().getIcStatus() == 4) {
                            icCardStatus = "已换卡";
                        } else if (mMemManagerData.getIcCard().getIcStatus() == 5) {
                            icCardStatus = "已作废";
                        }

                        tvIcCardNum.setText(mMemManagerData.getIcCard().getIcNo());
                        tvMemCardNum.setText(mMemManagerData.getIcCard().getMemberNo());
                        tvMemStatus.setText(icCardStatus);
                        tvPhoneNum.setText(mMemManagerData.getMemberPhone());
                        tvName.setText(mMemManagerData.getMemberName());
                        tvLevel.setText(mMemManagerData.getIcCard().getMemberGrade().getGradeName());
                        if (mMemManagerData.getIcCard().getIcStatus() == 1) {
                            onSetTitleCheckBg(null);
                            getSupportDelegate().replaceChildFragment(new MemCardManagerDefaultDelegate(), false);

                            tvMemGetCoin.setClickable(true);
                            tvMemSaveCoin.setClickable(true);
                            tvMemSaveTicket.setClickable(true);
                            tvMemTicketToOther.setClickable(true);
                            tvMemPointSet.setClickable(true);
                        } else {
                            tvMemGetCoin.setClickable(false);
                            tvMemSaveCoin.setClickable(false);
                            tvMemSaveTicket.setClickable(false);
                            tvMemTicketToOther.setClickable(false);
                            tvMemPointSet.setClickable(false);
                        }

//
//                        tvIcCardNum.setText(mCardSellInfoData.getCardSell().getIcNo());
//                        tvMemCardNum.setText(mCardSellInfoData.getCardSell().getMemberNo());

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        showShortToast(msg);
                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CardReadEvent event) {
        if (event != null && event.getData() != null) {
            isReadNow = false;
            if (StringUtil.isNotEmpty(event.getData().getString("cardNum"))) {
                if (event.getData().getChar("tag") == mCardIcTag) {
                    onGetCardInfo(event.getData().getString("cardNum").substring(0, 18));
                }
            } else {
                showShortToast("这个卡是空的");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        FusionCode.SCAN_TYPE = FusionCode.CANCEL;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
}
